
import { error } from "console";
import KHD from "../TS/Message/KHD";
import KHD2 from "../TS/Message/KHD2";
import state from "../TS/game/time/state";
import YP from "../sound/music";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DTTS extends cc.Component {
@property(Number)
  nn:number=0;
  isF(str: string): boolean {
    // 使用正则表达式匹配四位数字
    const regex = /^\d{4}$/;
    return regex.test(str);
  
  }
WZTS(str:string){
let a=cc.find("Canvas/WZTS")
a.active=true;
a.getComponent(cc.Label).string=str
setTimeout(()=>{a.active=false;},1000)


}

BC(str:string){
  
console.log(str)
  
  if(this.nn==0){}else{
  this.node.children[0].children[0].getComponent(cc.Label).string=str;
this.node.getComponent(cc.Button).interactable=false;
this.node.children[0].color=cc.color(200,200,200)

}
}
LK(){
  console.log(this.nn)

  
  if(this.nn!=0){
  this.node.children[0].children[0].getComponent(cc.Label).string="空缺";
this.node.getComponent(cc.Button).interactable=true;
this.node.children[0].color=cc.color(255,255,255)}

if (this.nn==7) {
  cc.find("Canvas/fangJian/p1").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p1").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
  cc.find("Canvas/fangJian/p3").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p3").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
  cc.find("Canvas/fangJian/p5").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p5").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
}
if (this.nn==8) {
  cc.find("Canvas/fangJian/p2").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p2").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
  cc.find("Canvas/fangJian/p4").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p4").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
  cc.find("Canvas/fangJian/p6").children[0].children[0].getComponent(cc.Label).string="空缺";
  cc.find("Canvas/fangJian/p6").getComponent(cc.Button).interactable=true;
  this.node.children[0].color=cc.color(255,255,255)
}

}

LC(){
  let a
if (KHD2.PT.length==1) {
 a=KHD2.PT[0]
}else if(KHD2.PT[0]==3){a=7}else{a=8}

let j=0
switch (this.nn) {
  case 7:KHD2.PT=[3,1,3,5];if(a==0){j=3}else if (a==8) {j=0    
  }else{j=2}
    
    break;
    case 8:KHD2.PT=[4,2,4,6];if(a==0){j=3}else if (a==7) {j=0    
    }else{j=2}
    
    break;
  default:KHD2.PT=[this.nn];if(a==0){j=1}else if (a==7||a==8) {j=-2    
  }else{j=0}
    
  break;
  
}
if (KHD2.PT[0]==0) {
  j=0
}

  const data = {
    id:2,
    error:0,
    playername:KHD2.Cname,
    nextlocation:this.nn,
    nowlocation:a,
    value:j,
  
  };




KHD2.socket.send(JSON.stringify(data))
}


kaiShi(){

console.log("xi")
const data = {
id:4

};




KHD2.socket.send(JSON.stringify(data))




}





dengLu(){
  let a=this.node.parent.children[0].children[1].getComponent(cc.Label).string
if(a.length>8){this.node.parent.children[0].children[1].getComponent(cc.Label).string=null;this.WZTS("名字太长了靓仔");}else{
//KHD2.inDT(a);
this.node.parent.active=false;
}
}


dinroom(){

let a= this.node.parent.children[0].children[1].getComponent(cc.Label).string
  let b=this.node.parent.children[1].children[1].getComponent(cc.Label).string
if(!this.isF(a)&&!this.isF(b)){
  this.node.parent.children[0].children[1].getComponent(cc.Label).string="";
  this.node.parent.children[1].children[1].getComponent(cc.Label).string="";
  this.WZTS("房间名和密码都是四位数哦");
} else{

KHD2.ConnectRoom(a,b)

}


}



dcroom(){
  let a= this.node.parent.children[0].children[1].getComponent(cc.Label).string
  let b=this.node.parent.children[1].children[1].getComponent(cc.Label).string
if(!this.isF(a)&&!this.isF(b)){
  this.node.parent.children[0].children[1].getComponent(cc.Label).string="";
  this.node.parent.children[1].children[1].getComponent(cc.Label).string="";
  this.WZTS("房间名和密码都是四位数哦");
} else{
KHD2.creatRoom(a,b)
//this.node.parent.active=false;
}
}

 quick_match()
{
  KHD2.Client.emit('socketname',KHD2.Cname);
  KHD2.Client.on("JRconnectroom",dat=>{
// KHD2.JR(dat[0],dat[0])
})


    KHD2.Client.emit('quick_match',['0','0','0','0'])

    KHD2.Client.on('quick_match_uuid',dat=>{
     KHD2.dat=dat
     console.log("hhh")
     KHD2.Client.emit('createroom',[  KHD2.dat, KHD2.dat,KHD2.Cname,1]);
    })
    

    KHD2.Client.on('createroomTF',(data:any)=>{
    console.log(data)
      switch (data) {
          case 'T':KHD2.FZ=true;   
           // KHD2.inRoom(KHD.dat,KHD.dat);
          
            console.log(KHD2.dat)
            cc.find("Canvas/New Node/New Label").getComponent(cc.Label).string="匹配中"
          
              
              break;
              case 'F':cc.find("Canvas/CHU").children[2].getComponent(DTTS).WZTS("kkk");
              
              break;
      
          default:
              break;
     
      
         }})
         
     //  KHD.inRoom(FJname,PassW)

 


    }


 text(){


  setTimeout(() => {
    cc.director.loadScene("SL", function() {
        // 将信息传递给下一个场景
        
            // 获取场景中的目标节点
          let myNode = cc.director.getScene().getChildByName('Canvas').getChildByName('thingsManager').getChildByName('AnimalManager')
            //let Node = cc.director.getScene().getChildByName('Canvas').getChildByName('UIManager').children[5].children[0].children[0]
            
           // console.log(myNode)
            // 获取目标节点上的目标脚本组件，将数据赋值给对应的属性



           // console.log(this.FAS)
          //  Node.getComponent(MUI).fashu=this.FAS.getComponent(fsc).allfs[KHD.PT-1]
          
           
      
        });
           
}, 1200);


}


start(): void {

if (this.nn==1) {
 KHD2.Cname=Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000+""
 //KHD2.indl()
  console.log("llla")
this.node.getComponent(YP).PlayAudioclip("byp", true, 0);

if (!KHD) {
  cc.find("Canvas/New Label").active=true
  cc.find("Canvas/New Label").getComponent(cc.Label).string="KHD没了"
}

} 

// cc.find("Canvas").getComponent("HPD").checkCb()
}



}
